<template>
	<div class="web_grid_box">
		<div class="card_preview" @click.stop="showImage">
			<img
				:src="`${store.config.domain}:${store.config.port}${bookmark.preview_image}`"
				alt="网站预览" />
		</div>

		<div class="card_content">
			<div class="card_header">
				<div class="card_text">
					<h3 class="card_title">{{ bookmark.title }}</h3>
					<div class="card_url">{{ bookmark.url }}</div>
				</div>
			</div>

			<p class="card_description">{{ bookmark.description }}</p>

			<div class="card_tags">
				<span
					v-for="(i, index) in tag"
					:key="index"
					class="card_tag"
					>{{ i }}</span
				>
			</div>

			<div class="card_footer">
				<span class="card_date">{{ format_date(bookmark.added_date) }}</span>
				<div class="card_actions">
					<button
						class="action_btn"
						@click.stop="editBookmark(bookmark)">
						编辑
					</button>
					<button
						class="action_btn"
						@click.stop="deleteBookmark(bookmark.id)">
						删除
					</button>
				</div>
			</div>
		</div>

		<el-image-viewer
			v-if="showPreview"
			:url-list="[
				`${store.config.domain}:${store.config.port}${bookmark.preview_image}`,
			]"
			show-progress
			:initial-index="0"
			:z-index="3"
			:max-scale="2"
			:min-scale="0.2"
			:teleported="true"
			:hide-on-click-modal="true"
			@close="showPreview = false" />
	</div>
</template>

<script setup>
	import { computed, ref } from "vue";
	import { useIndexStore } from "@/store/model/index";

	const store = useIndexStore();
	const props = defineProps({
		bookmark: {
			type: Object,
			required: true,
		},
		tags: {
			type: Array,
			required: true,
		},
	});
	const emit = defineEmits(["editBookmark", "deleteBookmark"]);

	const tag = computed(() => {
		let list = [];
		props.tags.forEach(item => {
			props.bookmark.tags.forEach(tag => {
				if (item.id === tag) {
					list.push(item.name);
				}
			});
		});
		return list;
	});
	const showPreview = ref(false);

	// 格式化日期函数，这里简单返回日期字符串，可按需修改
	const format_date = date => new Date(date).toLocaleDateString();

	const editBookmark = book => {
		emit("editBookmark", book);
	};

	const deleteBookmark = id => {
		emit("deleteBookmark", id);
	};
	const showImage = ()=>{
		showPreview.value = true;
	}
</script>

<style lang="less" scoped>
	.web_grid_box {
		width: 365px;
		background-color: #ffffff;
		border-radius: 12px;
		box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06);
		border: 1px solid #e5e7eb;
		overflow: hidden;
		transition: all 0.2s ease; // 添加过渡效果

		&:hover {
			box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1); // 增强阴影实现悬浮效果
			transform: translateY(-4px); // 向上移动实现悬浮效果
		}

		.card_preview {
			height: 144px;
			background-color: #f3f4f6;
			position: relative;
			overflow: hidden;

			img {
				width: 100%;
				height: 100%;
				object-fit: cover;
			}

			.checkbox_container {
				position: absolute;
				top: 8px;
				left: 8px;

				input {
					width: 16px;
					height: 16px;
					border-radius: 4px;
					cursor: pointer;
				}
			}
		}

		.card_content {
			padding: 16px;

			.card_header {
				display: flex;
				align-items: flex-start;
				flex-wrap: nowrap;
				margin-bottom: 8px;

				.card_favicon {
					width: 32px;
					height: 32px;
					background-color: #f3f4f6;
					border-radius: 4px;
					overflow: hidden;

					img {
						width: 100%;
						height: 100%;
						object-fit: cover;
					}
				}

				.card_text {
					margin-left: 8px;
					flex: 1;
					min-width: 0;

					.card_title {
						margin: 0;
						font-size: 16px;
						color: #1f2937;
						white-space: nowrap; // 禁止换行
						overflow: hidden; // 隐藏溢出内容
						text-overflow: ellipsis; // 溢出内容用省略号表示
					}

					.card_url {
						font-size: 12px;
						color: #6b7280;
						text-decoration: none;

						&:hover {
							text-decoration: underline;
						}
					}
				}
			}

			.card_description {
				font-size: 14px;
				color: #374151;
				margin-bottom: 12px;
			}

			.card_tags {
				margin-bottom: 12px;

				.card_tag {
					font-size: 12px;
					color: #3b82f6;
					background-color: #eff6ff;
					padding: 2px 8px;
					border-radius: 4px;
					margin-right: 4px;
				}
			}

			.card_footer {
				display: flex;
				justify-content: space-between;
				align-items: center;

				.card_date {
					font-size: 12px;
					color: #6b7280;
				}

				.card_actions {
					display: flex;

					.action_btn {
						font-size: 12px;
						color: #3b82f6;
						background-color: transparent;
						border: none;
						cursor: pointer;
						padding: 4px 8px;
						border-radius: 4px;

						&:hover {
							background-color: #eff6ff;
						}

						&:not(:last-child) {
							margin-right: 8px;
						}
					}
				}
			}
		}
	}
</style>
